﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Wicresoft.SelfService.DAO;
using Wicresoft.SelfService.BL;
using Wicresoft.SelfService.BL.Interface;
using Wicresoft.SelfService.DAL;

namespace Wicresoft.SelfService.Portal.Controllers
{
    public class BDMaintainController : BaseController
    {
        private string ARServer;
        private string Mid_Base;
        private string appSystemName;
        public ITServiceHelper helper { get; private set; }
        public Wicresoft.SelfService.BL.Interface.IBreakDown IBreakDown { get; private set; }
        public IUserSync userSync { get; private set; }
        public Wicresoft.SelfService.BL.Interface.IAppSystem myAppSystem { get; private set; }
        public static WFService.ApproveFlowClient IApproveFlow { get; private set; }
        protected override void Initialize(RequestContext requestContext)
        {
            if (helper == null) { helper = new ITServiceHelper(); }
            if (IBreakDown == null) { IBreakDown = new Wicresoft.SelfService.BL.BreakDown(); }
            if (userSync == null) { userSync = new UserSync(); }
            if (myAppSystem == null) { myAppSystem = new MyAppSystem(); }
            if (IApproveFlow == null) { IApproveFlow = new WFService.ApproveFlowClient(); }

            ARServer = ConfigurationManager.AppSettings["ARServer"];
            Mid_Base = ConfigurationManager.AppSettings["ITSM_Mid_Tier"];//without end "/"
            appSystemName = ConfigurationManager.AppSettings["ApplicationSystemName"];
            if (string.IsNullOrEmpty(appSystemName)) appSystemName = "应用系统";
            base.Initialize(requestContext);
        }
        //
        // GET: /BDMaintain/

        [Authorize]
        public ActionResult BreakMaintain()
        {
            Dictionary<int, List<BreakConfig>> model = helper.getBreakMenuData();
            return View(model);
        }

        [Authorize]
        public ActionResult ShowBreakDetail(int ID)
        {
            var Lvl1List = helper.getAllFirstLvlBreakInfo().Select(o => o.LvlName);
            ViewData["lvl1"] = new SelectList(Lvl1List.ToList());

            //List<BreakConfig> model = helper.getBreakDetail(ID);
            var model = helper.GetBreakDownsToOne(ID);
            return PartialView(model);
        }

        [Authorize]
        public ActionResult UpdateBreak(BreakDowmModel model)
        {
            bool flag = helper.updateBreak(model);
            return Content(flag ? "true" : "false");
        }

        //[HttpPost]
        //public ActionResult DeleteBreak(int id, int parentid)
        //{
        //    //bool isOk = helper.deleteBreak(id, parentid);
        //    bool isOK = helper.deletechild(id);
        //    return Content(isOK ? "true" : "false");
        //}

        [HttpPost]
        public ActionResult DeleteBreak(int id)
        {
            bool isOK = helper.deletechild(id);
            return Content(isOK ? "true" : "false");
        }

        public ActionResult CreateBreak()
        {
            var model = helper.getAllFirstLvlBreakInfo();
            //Dictionary<int, string> listLvl1 = new Dictionary<int, string>();
            //foreach (var item in model) 
            //{
            //    listLvl1.Add(item.ID, item.LvlName);
            //}
            ViewData["lvl1"] = model;
            return View();
        }

        [HttpPost]
        public ActionResult CreateBreak(BreakDowmModel model)
        {
            BreakConfig b1 = new BreakConfig();
            BreakConfig b2 = new BreakConfig();
            BreakConfig b3 = new BreakConfig();

            bool isOk=false;
            b1.LvlName = model.Tier1;
            int m = helper.createBreakdwon(b1, 1);
            if (m != 0)
            {
                b2.LvlName = model.Tier2;
                b2.Parentid = m;
                int m1 = helper.createBreakdwon(b2, 2);
                if (m1 != 0)
                {
                    if (model.Tier3 != null)
                    {
                        b3.LvlName = model.Tier3;
                        b3.Parentid = m1;
                        int m2 = helper.createBreakdwon(b3, 3);
                    }
                    isOk = true;
                }
            }
            return Content(isOk ? "true" : "false");
        }

        [HttpPost]
        public ActionResult SearchByTier1(BreakDowmModel model) 
        {
            string applvl1 = model.Tier1;
            bool flag = helper.searchByAppLvl1(applvl1);
            return Content(flag ? "true" : "false");
        }


        [HttpPost]
        public ActionResult SearchByTier2(BreakDowmModel model)
        {
            var config = helper.getBreakDwonbyName(model.Tier1);
            bool flag = true;
            if (config.ID != 0)
                flag = helper.searchByAppLvl2(config.ID, model.Tier2);
            return Content(flag ? "true" : "false");
        }





        public string createCodeNumber()
        {
            return helper.getServiceRecordNo();
        }
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            if (this.helper != null)
                helper.Dispose();
            if (IBreakDown != null && IBreakDown is Wicresoft.SelfService.BL.BreakDown)
            {
                ((Wicresoft.SelfService.BL.BreakDown)IBreakDown).Dispose();
            }

            if (userSync != null && userSync is UserSync)
            {
                ((UserSync)userSync).Dispose();
            }
        }

    }
}
